package com.kele.search.config;

import org.apache.http.HttpHost;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestClientBuilder;
import org.elasticsearch.client.RestHighLevelClient;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.ArrayList;
import java.util.List;

@Configuration
public class ElasticSearchConfig {

    @Value("${kele.elasticsearch.host}")
    private String host;

    @Bean
    public RestHighLevelClient restHighLevelClient(){
        //1.连接rest接口
        String[] hostSplit = host.split(",");
        List<HttpHost> hostList = new ArrayList<>();
        for(String host:hostSplit){
            String[] split = host.split(":");
            HttpHost http=new HttpHost(split[0],Integer.parseInt(split[1]),"http");
            hostList.add(http);
        }

        RestClientBuilder builder= RestClient.builder(hostList.toArray(new HttpHost[0]));//rest构建器
        RestHighLevelClient restHighLevelClient=new RestHighLevelClient(builder);//高级客户端对象
        return restHighLevelClient;
    }
}
